# 导入必要的库
import os
import numpy as np
import logging
from collections import Counter
from sklearn.model_selection import train_test_split
from lazypredict.Supervised import LazyClassifier
from util import createXY

# 配置logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

# 配置参数
def get_config():
    """
    获取配置参数
    """
    config = {
        'feature': 'flat',  # 固定使用flat特征提取
        'train_dir': '../data/train',  # 训练集目录
        'dest_dir': '.',  # 输出目录
    }
    return config

# 使用Lazypredict评估模型
def evaluate_with_lazypredict(X_train, X_test, y_train, y_test):
    """
    使用Lazypredict快速评估多种模型的性能
    """
    logging.info("开始使用Lazypredict评估模型...")
    clf = LazyClassifier(verbose=0, ignore_warnings=True, custom_metric=None)
    models, predictions = clf.fit(X_train, X_test, y_train, y_test)
    logging.info("Lazypredict模型评估完成。")
    print(models)
    return models

# 主函数
def main():
    config = get_config()
    
    logging.info(f"特征提取方法: {config['feature'].upper()}")
    logging.info(f"训练集目录: {config['train_dir']}")
    logging.info(f"缓存输出目录: {config['dest_dir']}")

    # 载入和预处理数据
    X, y = createXY(train_folder=config['train_dir'], dest_folder=config['dest_dir'], method=config['feature'])
    X = np.array(X).astype('float32')
    y = np.array(y)
    logging.info("数据加载和预处理完成。")
    logging.info(f"数据集大小: {X.shape}, 标签分布: {Counter(y)}")

    # 数据集分割
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=2023)
    logging.info("数据集划分为训练集和测试集。")
    logging.info(f"训练集大小: {X_train.shape}, 测试集大小: {X_test.shape}")

    # 使用Lazypredict评估模型
    evaluate_with_lazypredict(X_train, X_test, y_train, y_test)

if __name__ == '__main__':
    main()
